var index_tab_menu = new HashMap();

var index_data_order = 0;

var index_user_id;

$(document).ready(function ()
{
	
	index_initEvent();
	
	index_menu_list();
	
	index_initView();
	
	index_addTab1("/carfee/web_map.html" ,"首页");
	
});

function index_initEvent()
{
	//  -------------菜单的点击事件
	var elm = $("#side_nav");
	$(elm).on('click', '.navlist > li > a', function () {
        if ( $('body').hasClass('main-side-collapsed') ) {
          return;
        }
        var parent = $(this).parent();
        if ( parent.hasClass('active') ) {
          parent.removeClass('active').find('.sub-nav').show().slideUp(200);
        } else {
          parent.addClass('active').find('.sub-nav').hide().slideDown(200);
          parent.siblings('.active').removeClass('active').find('.sub-nav').show().slideUp(200);
        }
      });
      // .crt
      $(elm).on('click', '.navlist .sub-nav a, .navlist > li.nosub > a', function () {
        $(elm).find('.navlist .sub-nav a, .navlist > li.nosub > a').removeClass('crt');
        $(this).addClass('crt');
      });
      // 收起状态动画
      $(elm).on('mouseenter', '.navlist > li', function () {
        if ( !$('body').hasClass('main-side-collapsed') ) {
          return;
        }
        $(this).find('> a span, .sub-nav').addClass('a-nav');
      }).on('mouseleave', '.navlist > li', function () {
        if ( !$('body').hasClass('main-side-collapsed') ) {
          return;
        }
        $(this).find('> a span, .sub-nav').removeClass('a-nav');
      });
      
      
      // --------------     菜单的收起与展开
      var element = $("#main_header");
      // 侧边栏展开收起
      $(element).on('click', '.toggle-btn', function () {
        $('body').toggleClass('main-side-collapsed');
        if ($('body').hasClass("main-side-collapsed"))
        {
        	$("#brand_a").text("图");
        }
        else
        {
        	$("#brand_a").text("图书馆运营管理平台");
        }
      });

      // 侧边菜单展开收缩
      $(element).on('click', '.side-menu > li', function (evt) {
        evt.stopPropagation();
        $(this).toggleClass('active').siblings().removeClass('active');
      });
      // 点击空白区域收起侧边菜单
      $(document).on('click', function () {
        $(element).find('.side-menu > li').removeClass('active');
      });
      
      
      // 退出
      $("#logout").click(function (){
    	//index_confirm("确定要退出么?" ,"index_logout" ,"");
		$("body").umDialog("confirm",{
			content:"确定要退出么?",
			enter_method:function (self){
				index_logout();
			}
		});
      });
      
      // 个人中心
      $("#personal_center").click(function (){
  		var obj = new Object();
		obj.userId = index_user_id;
  		um_ajax_get(
				INDEX_WEBAPP + "/sysuser/detail.do",
				obj,
				function (data){
					$('body').umDialog("alert",{
						content:$("#alert_personal_div").html(),
						width:450,
						left:"40%",
						top:"23%"
					});
					$("#dialog").find("[data-id=user_account]").text(data.sysUser.user_account);
					$("#dialog").find("[data-id=user_name]").text(data.sysUser.user_name);
					$("#dialog").find("[data-id=user_work_num]").text(data.sysUser.user_work_num);
				},
				null);
//			$('body').umDialog("alert",{
//				content:$("#alert_personal_div").html(),
//				width:450,
//				left:"40%",
//				top:"23%"
//			});
      });
      
      // tab 菜单点击事件
      $("#tab-ul").delegate("li" ,"click" ,function (){
    	  $("#tab-ul").find("li").removeClass("tab-ul-selected");
    	  $(this).addClass("tab-ul-selected");
    	  $("#iframe_list_div").find("iframe").hide();
    	  $("#iframe_list_div").find("iframe[id='frame"+$(this).attr("data-id")+"']").show();
      });
      
      // tab 删除事件
      $("#tab-ul").delegate("[data-id=tab_close]" ,"click" ,function (){
    	  index_deleteTab(this);
    	  return false;
      });
}

function index_initView()
{
	// 获取用户昵称
	$.ajax({
		type: "GET",
		url: INDEX_WEBAPP + "/user/select.do",
		dataType: "json",
		success :function(data)
		{
		   $("#nick_name").text(data.userName);
		   $("#user_name").text(data.userMobile);
		   index_user_id = data.userId;
		}
	});
	
	// 显示时间
	index_getDate();
}

// 登出
function index_logout()
{
	$.ajax({
		type: "POST",
		url: INDEX_WEBAPP + "/user/logout.do",
		dataType: "json",
		success :function(data)
		{
			if (data.flag ==  SUCCESS)
			{
				window.location.href = "/login.html";
			}
		}
	});
}

// 获取用户菜单
function index_menu_list()
{
	$.ajax({
		type: "GET",
		url: INDEX_WEBAPP + "/user/menu/select.do",
		dataType: "json",
		success :function(data)
		{
			_render(data);
		}
	});
	
	function _render(data)
	{
		if (data)
		{
			var buffer = [];
			var tmp_parentId;
			var menu_order = 0;
			for (var i=0;i<data.length;i++)
			{
				buffer = [];
				if (data[i].parentId == -1)
				{
					if (data[i].menuName == "首页")
					{
						buffer.push('<li id="menu'+menu_order+'" data-type="menu" data-id="/carfee/web_map.html">');
						buffer.push('<a href="#">');
						buffer.push('<i class="fa fa-th-list"></i>');
						buffer.push('<span>'+data[i].menuName+'</span>');
						buffer.push('</a><ul class="sub-nav"></ul></li>');
						$("#navlist").append(buffer.join(""));
						tmp_parentId = i;
					}
					else
					{
						buffer.push('<li id="'+(i)+'">');
						buffer.push('<a href="#">');
						buffer.push('<i class="fa fa-th-list"></i>');
						buffer.push('<span>'+data[i].menuName+'</span>');
						buffer.push('</a><ul class="sub-nav"></ul></li>');
						$("#navlist").append(buffer.join(""));
						tmp_parentId = i;
					}

				}
				else
				{
					buffer.push('<li id="menu'+menu_order+'" data-type="menu" data-id="'+data[i].menuUrl+'">');
					buffer.push('<a href="#">');
					buffer.push('');
					buffer.push('<span>'+data[i].menuName+'</span>');
					buffer.push('</a></li>');
					$("[id='"+(tmp_parentId)+"']").find("ul").append(buffer.join(""));
					
					$("#navlist").find("[id='menu"+menu_order+"']").click(function (){
						index_addTab(this);
					});
					menu_order++;
					
				}
			}
			//默认选中第一个菜单
//			$("#navlist").find("li").eq(0).click();
//			$("#navlist").find("li").eq(0).attr("class" ,"nosub active");
		}
	}
}

function index_addTab(liObj)
{
	$("#tab-ul").find("li").removeClass("tab-ul-selected");
	$("#iframe_list_div").find("iframe").hide();
	var buffer = [];
	buffer.push('<li class="tab-ul-selected" data-id="'+index_data_order+'">');
	buffer.push('<span>'+$(liObj).find("span").html()+'</span>');
	buffer.push('<div class="tab_close" data-id="tab_close"></div>');
	buffer.push('</li>');
	$("#tab-ul").append(buffer.join(""));
	var iframe = $('<iframe class="content" src="'+$(liObj).attr("data-id")+'" id="frame'+index_data_order+'"style="width:100%;height:100%;border:0;display:none">');
	iframe.appendTo($("#iframe_list_div"));
	iframe.show();
	index_data_order++;
}

function index_addTab1(url ,content)
{
	$("#tab-ul").find("li").removeClass("tab-ul-selected");
	$("#iframe_list_div").find("iframe").hide();
	var buffer = [];
	buffer.push('<li class="tab-ul-selected" data-id="'+index_data_order+'">');
	buffer.push('<span>'+content+'</span>');
	buffer.push('<div class="tab_close" data-id="tab_close"></div>');
	buffer.push('</li>');
	$("#tab-ul").append(buffer.join(""));
	var iframe = $('<iframe src="'+url+'" id="frame'+index_data_order+'"style="width:100%;height:100%;border:0;display:none">');
	iframe.appendTo($("#iframe_list_div"));
	iframe.show();
	index_data_order++;
}

function index_deleteTab(liObj)
{
	var order_num = $(liObj).parent().attr("data-id");
	$(liObj).parent().prev().click();
	$("#frame" + order_num).remove();
	$(liObj).parent().remove();
}

function index_getDate(){
	var week; 
	var date = new Date();
	if(date.getDay()==0)          week="星期日" 
	if(date.getDay()==1)          week="星期一" 
	if(date.getDay()==2)          week="星期二" 
	if(date.getDay()==3)          week="星期三" 
	if(date.getDay()==4)          week="星期四" 
	if(date.getDay()==5)          week="星期五" 
	if(date.getDay()==6)          week="星期六"
	
	$("#systime").text(date.getFullYear()+"年"+(date.getMonth() + 1)+"月"+date.getDate()+"号 " + week);
}

